import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)

export const store = new Vuex.Store({
    state: {
        isLogin: false,
        token: '',
        user: {},
        userId: '',
        $server: 'https://app.jingziy.cn',
        // $server: 'https://app.global3plus.com',
        $serverMap: {
            zh: 'https://app.jingziy.cn',
            hk: 'https://www.global3pro.com'
        },
        $image: 'https://app.jingziy.cn',
        $imageMap: {
            zh: 'https://app.jingziy.cn',
            hk: 'https://www.global3pro.com'
        },
        // $server: 'https://app.global3plus.com',
        // $serverMap: {
        //     zh: 'https://app.global3plus.com',
        //     hk: 'https://app.global3plus.com'
        // },
        // $image: 'https://app.global3plus.com',
        // $imageMap: {
        //     zh: 'https://app.global3plus.com',
        //     hk: 'https://app.global3plus.com'
        // },
        locale: 'zh',
        __key_user: 'usemall_member',
        __key_token: 'usemall_token',
        __key_locale: 'usemall_locale',
        __key_user_id: 'usemall_user_id',
        address_id: ''
    },
    getters: {
        get_address(state) {
            return state.address_id
        },
        get_user(state) {
            return state.user
        },
        head_pic(state) {
            const head_pic = state.user.head_pic
            const http = /http/.test(head_pic)
            let result = ''
            if (head_pic && http) {
                result = head_pic
            } else if (head_pic && !http) {
                result = state.$server + head_pic
            }
            // console.log('state head_pic ', result)
            return result
        }
    },
    mutations: {
        // 登录成功
        login(state, user) {
            // 用户已登录
            state.isLogin = true

            state.user = user
            // 存储会员 member 数据
            uni.setStorage({
                key: state.__key_user,
                data: state.user
            })

            state.userId = user.user_id
            uni.setStorage({
                key: state.__key_user_id,
                data: state.userId
            })
        },
        // 注销
        logout(state) {
            state.isLogin = false

            state.user = {}
            uni.removeStorage({
                key: state.__key_user
            })

            state.userId = ''
            uni.removeStorage({
                key: state.__key_user_id
            })

            // state.token = ''
            // uni.removeStorage({
            //     key: state.__key_token
            // })
        },
        // token 令牌
        setToken(state, token) {
            state.token = token
            // 存储uid token 数据
            uni.setStorage({
                key: state.__key_token,
                data: state.token
            })
        },
        // 加载 Token
        loadToken(state) {
            state.user = uni.getStorageSync(state.__key_user)
            state.token = uni.getStorageSync(state.__key_token)
            state.locale = uni.getStorageSync(state.__key_locale)
            // state.token_expired = uni.getStorageSync(state.__key_token_expired)
            // const expired = new Date().getTime() + state.token_expired * 1000
            // const a = state.token_expired - new Date().getTime()
            // console.log('state.token_expired', a / 1000 / 60 / 60 / 24)
            // if (state.token_expired > new Date().getTime()) {
            //     state.islogin = true
            // }
        },
        setLocale(state, val) {
            state.locale = val
            state.$server = state.$serverMap[val]
            state.$image = state.$imageMap[val]
            uni.setStorage({
                key: state.__key_locale,
                data: state.locale
            })
        },
        setUserId(state, val) {
            state.userId = val
            uni.setStorage({
                key: state.__key_user_id,
                data: state.userId
            })
        },
        set_address(state, val) {
            state.address_id = val
        },
        set_user(state, val) {
            state.user = val
        }
    },
    actions: {
        set_address({
            commit
        }, val) {
            commit('set_address', val)
        },
        set_user({
            commit
        }, val) {
            commit('set_user', val)
        },
        get_locale({
            commit,
            state
        }) {
            const val = uni.getStorageSync(state.__key_locale) || state.locale
            commit('setLocale', val)
        },
        async get_token({
            commit
        }) {
            const res = await Vue.prototype.$http('get|api/Index/index')
            commit('setToken', res.result.session_id)
        },
        async getUserInfo({
            commit
        }) {
            await Vue.prototype.$http('get|api/User/index').then(res => {
                commit('login', res.result.users)
            })
        }
    }
})
